package com.avic.rong.activity.interest;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.avic.rong.BaseActivity;

import com.avic.rong.MainActivity;
import com.avic.rong.R;
import com.avic.rong.adapter.FocusAreaAdapter;
import com.avic.rong.api.mine.InterestedApi;
import com.avic.rong.bean.mine.InterestedBean;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

//第一次启动app感兴趣的分类
public class FocusFirstActivity extends BaseActivity {


    @BindView(R.id.recv_focus)
    RecyclerView recv_focus;
    @BindView(R.id.btn_skip)
    Button btn_skip;
    @BindView(R.id.btn_complete)
    Button btn_complete;
    @BindView(R.id.loading_layout)
    LoadingLayout loadingLayout;
    private ArrayList<InterestedBean.InterestListBean> mList = new ArrayList<>();
    private ArrayList<String> strings = new ArrayList<>();
    private ArrayList<String> labels = new ArrayList<>();
    private FocusAreaAdapter focusAreaAdapter;
    private StringBuffer sb = new StringBuffer();
    private InterestedApi interestedApi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



        //是否第一次启动
        boolean firstStart = SharedPreferencesUtil.getInstance(this).getFirstStart();

        if (firstStart){
            SharedPreferencesUtil.getInstance(this).setFirstStart(false);
        }else {
            Intent intent = MainActivity.queryIntent(this);
//            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
            finish();
        }

        loadingLayout.setStatus(LoadingLayout.LOADING);

        loadingLayout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                HttpManager.getInstance().doHttpDeal(interestedApi);
                loadingLayout.setStatus(LoadingLayout.LOADING);
            }

            @Override
            public void onReturn(View v) {
                onBackPressed();
            }
        });

    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_focus_first;
    }

    @Override
    protected void initView() {
        isNeedTitle = false;

        focusAreaAdapter = new FocusAreaAdapter(this, mList);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int i) {
                int viewType = focusAreaAdapter.getItemViewType(i);
                if (viewType == 1) {
                    return 2;
                }
                return 1;
            }
        });
        recv_focus.setLayoutManager(gridLayoutManager);
        recv_focus.setAdapter(focusAreaAdapter);
        initData();

    }

    @Override
    protected void addListener() {

    }

    private void initData() {
        interestedApi = new InterestedApi(this, interestedListener);
        interestedApi.setToken(SharedPreferencesUtil.getLoginToken());
        HttpManager.getInstance().doHttpDeal(interestedApi);
    }

    HttpOnNextListener<BaseResultEntity> interestedListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_200)) {
                InterestedBean data = (InterestedBean) baseResultEntity.getData();
                List<InterestedBean.InterestListBean> interestList = data.getInterestList();
                loadingLayout.setStatus(LoadingLayout.SUCCESS);

                mList.addAll(interestList);
                focusAreaAdapter.notifyDataSetChanged();

            }else {
                loadingLayout.setStatus(LoadingLayout.ERROR);
            }
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            if (e instanceof SocketTimeoutException || e instanceof ConnectException) {
                loadingLayout.setStatus(LoadingLayout.NO_NETWORK);
            } else {
                loadingLayout.setStatus(LoadingLayout.ERROR);
            }
        }
    };

    @OnClick({R.id.btn_skip, R.id.btn_complete})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.btn_skip://跳过
                startActivity(MainActivity.queryIntent(this));
                finish();
                break;
            case R.id.btn_complete://完成
                strings = focusAreaAdapter.getStrings();
                labels = focusAreaAdapter.getLabels();
                if (strings.size()==0){//未选择任何分类
                    ToastTools.showPublicToast("您还未选择感兴趣的分类");
                }else {//把集合传给首页
                    SharedPreferencesUtil.getInstance().put("interest",strings);
                    for (int i = 0; i < labels.size(); i++) {
                        if (i==labels.size()-1){
                            sb.append(labels.get(i));
                        }else {
                            sb.append(labels.get(i)+",");
                        }
                    }
                    String string = sb.toString();
                    SharedPreferencesUtil.put("upinter",string);
                    startActivity(MainActivity.queryIntent(FocusFirstActivity.this));
                    finish();
                }
                break;
        }
    }

    public static Intent queryIntent(Context context){
        Intent intent = new Intent(context,FocusFirstActivity.class);
        return  intent;
    }
}
